package com.yuuzy.view;

import com.yuuzy.dao.Buy_InsuranceDAO;
import com.yuuzy.dao.ClientDAO;
import com.yuuzy.dao.InsuranceDAO;
import com.yuuzy.model.Buy_Insurance;
import com.yuuzy.model.Client;
import com.yuuzy.model.Insurance;
import com.yuuzy.utils.StringUtils;

import javax.swing.*;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import java.util.Vector;

public class BuyInsurance extends JInternalFrame {
    private JTable insurance_table;
    private JTextField BI_nameText;

    private InsuranceDAO insuranceDAO = new InsuranceDAO();
    private Buy_InsuranceDAO buy_insuranceDAO = new Buy_InsuranceDAO();

    private JTextField insurance_idText;
    private JTextField insurance_nameText;
    private JTextField insurance_yearText;
    private JTextField insurance_personText;
    private JTextField insurance_projectText;
    private JTextField insurance_amountText;
    private JTextField client_nameText;
    private JTextField client_passwordText;

    /**
     * Create the frame.
     */
    public BuyInsurance() {
        setClosable(true);
        setIconifiable(true);
        setTitle("购买保险");
        setBounds(100, 100, 1000, 1100);

        JScrollPane scrollPane = new JScrollPane();

        JLabel label = new JLabel("保险名称：");
        label.setFont(new Font("宋体", Font.PLAIN, 20));

        BI_nameText = new JTextField();
        BI_nameText.setFont(new Font("宋体", Font.PLAIN, 20));
        BI_nameText.setColumns(10);

        JButton button = new JButton("查询 ");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                queryActionPerformed(e);
            }
        });
        button.setIcon(new ImageIcon(BuyInsurance.class.getResource("/image/pm-query-16.png")));
        button.setFont(new Font("宋体", Font.PLAIN, 20));

        JPanel panel = new JPanel();
        panel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "\u8D2D\u4E70\u4FDD\u9669\u4EA7\u54C1", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 120, 215)));

        JLabel label_1 = new JLabel("保险编号：");

        insurance_idText = new JTextField();
        insurance_idText.setText("");
        insurance_idText.setFont(new Font("宋体", Font.PLAIN, 18));
        insurance_idText.setEnabled(true);
        insurance_idText.setEditable(false);
        insurance_idText.setColumns(10);

        JLabel label_2 = new JLabel("保险名称：");

        insurance_nameText = new JTextField();
        insurance_nameText.setEditable(false);
        insurance_nameText.setColumns(10);

        JLabel label_3 = new JLabel("保险年限：");

        insurance_yearText = new JTextField();
        insurance_yearText.setEditable(false);
        insurance_yearText.setColumns(10);

        JLabel label_4 = new JLabel("适用人群：");

        insurance_personText = new JTextField();
        insurance_personText.setEditable(false);
        insurance_personText.setColumns(10);

        JButton button_1 = new JButton("购买 ");
        button_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                buyActionPerformed(e);
            }
        });
        button_1.setIcon(new ImageIcon(BuyInsurance.class.getResource("/image/Buy.png")));

        JLabel label_5 = new JLabel("保险项目：");

        insurance_projectText = new JTextField();
        insurance_projectText.setEditable(false);
        insurance_projectText.setColumns(10);

        JLabel label_7 = new JLabel("保险价格：");

        insurance_amountText = new JTextField();
        insurance_amountText.setEditable(false);
        insurance_amountText.setColumns(10);

        JLabel label_6 = new JLabel("用户姓名：");

        client_nameText = new JTextField();
        client_nameText.setColumns(10);

        JLabel label_8 = new JLabel("用户密码：");

        client_passwordText = new JTextField();
        client_passwordText.setColumns(10);
        GroupLayout gl_panel = new GroupLayout(panel);
        gl_panel.setHorizontalGroup(
                gl_panel.createParallelGroup(Alignment.TRAILING)
                        .addGroup(gl_panel.createSequentialGroup()
                                .addGap(75)
                                .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
                                        .addGroup(gl_panel.createSequentialGroup()
                                                .addComponent(label_1)
                                                .addPreferredGap(ComponentPlacement.RELATED)
                                                .addComponent(insurance_idText, GroupLayout.PREFERRED_SIZE, 105, GroupLayout.PREFERRED_SIZE))
                                        .addGroup(gl_panel.createSequentialGroup()
                                                .addPreferredGap(ComponentPlacement.RELATED)
                                                .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
                                                        .addGroup(gl_panel.createSequentialGroup()
                                                                .addComponent(label_5, GroupLayout.PREFERRED_SIZE, 90, GroupLayout.PREFERRED_SIZE)
                                                                .addPreferredGap(ComponentPlacement.RELATED)
                                                                .addComponent(insurance_projectText, GroupLayout.PREFERRED_SIZE, 197, GroupLayout.PREFERRED_SIZE))
                                                        .addGroup(gl_panel.createSequentialGroup()
                                                                .addComponent(label_7, GroupLayout.PREFERRED_SIZE, 90, GroupLayout.PREFERRED_SIZE)
                                                                .addPreferredGap(ComponentPlacement.RELATED)
                                                                .addComponent(insurance_amountText, GroupLayout.PREFERRED_SIZE, 197, GroupLayout.PREFERRED_SIZE))
                                                        .addGroup(gl_panel.createSequentialGroup()
                                                                .addComponent(label_6, GroupLayout.PREFERRED_SIZE, 90, GroupLayout.PREFERRED_SIZE)
                                                                .addPreferredGap(ComponentPlacement.RELATED)
                                                                .addComponent(client_nameText, GroupLayout.PREFERRED_SIZE, 197, GroupLayout.PREFERRED_SIZE)))))
                                .addGap(54)
                                .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
                                        .addGroup(gl_panel.createSequentialGroup()
                                                .addComponent(label_2)
                                                .addPreferredGap(ComponentPlacement.RELATED)
                                                .addComponent(insurance_nameText, GroupLayout.PREFERRED_SIZE, 197, GroupLayout.PREFERRED_SIZE))
                                        .addGroup(gl_panel.createSequentialGroup()
                                                .addComponent(label_4, GroupLayout.PREFERRED_SIZE, 90, GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(ComponentPlacement.RELATED)
                                                .addComponent(insurance_personText, GroupLayout.PREFERRED_SIZE, 197, GroupLayout.PREFERRED_SIZE))
                                        .addGroup(gl_panel.createSequentialGroup()
                                                .addPreferredGap(ComponentPlacement.RELATED)
                                                .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
                                                        .addGroup(gl_panel.createSequentialGroup()
                                                                .addComponent(label_8, GroupLayout.PREFERRED_SIZE, 90, GroupLayout.PREFERRED_SIZE)
                                                                .addPreferredGap(ComponentPlacement.RELATED)
                                                                .addComponent(client_passwordText, GroupLayout.PREFERRED_SIZE, 197, GroupLayout.PREFERRED_SIZE))
                                                        .addGroup(gl_panel.createSequentialGroup()
                                                                .addComponent(label_3, GroupLayout.PREFERRED_SIZE, 90, GroupLayout.PREFERRED_SIZE)
                                                                .addPreferredGap(ComponentPlacement.RELATED)
                                                                .addComponent(insurance_yearText, GroupLayout.PREFERRED_SIZE, 197, GroupLayout.PREFERRED_SIZE)))))
                                .addContainerGap(81, Short.MAX_VALUE))
                        .addGroup(gl_panel.createSequentialGroup()
                                .addContainerGap(342, Short.MAX_VALUE)
                                .addComponent(button_1, GroupLayout.PREFERRED_SIZE, 130, GroupLayout.PREFERRED_SIZE)
                                .addGap(324))
        );
        gl_panel.setVerticalGroup(
                gl_panel.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_panel.createSequentialGroup()
                                .addGap(71)
                                .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(label_1)
                                        .addComponent(label_2)
                                        .addComponent(insurance_nameText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(insurance_idText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(ComponentPlacement.RELATED, 58, Short.MAX_VALUE)
                                .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(label_4)
                                        .addComponent(insurance_personText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(label_5)
                                        .addComponent(insurance_projectText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addGap(62)
                                .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(insurance_amountText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(label_3)
                                        .addComponent(insurance_yearText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(label_7))
                                .addGap(63)
                                .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(label_6)
                                        .addComponent(client_nameText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(label_8)
                                        .addComponent(client_passwordText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addGap(68)
                                .addComponent(button_1)
                                .addGap(71))
        );
        panel.setLayout(gl_panel);
        GroupLayout groupLayout = new GroupLayout(getContentPane());
        groupLayout.setHorizontalGroup(
                groupLayout.createParallelGroup(Alignment.LEADING)
                        .addGroup(groupLayout.createSequentialGroup()
                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addGap(200)
                                                .addComponent(label, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE)
                                                .addGap(34)
                                                .addComponent(BI_nameText, GroupLayout.PREFERRED_SIZE, 264, GroupLayout.PREFERRED_SIZE)
                                                .addGap(71)
                                                .addComponent(button, GroupLayout.PREFERRED_SIZE, 116, GroupLayout.PREFERRED_SIZE))
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addGap(83)
                                                .addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
                                                        .addComponent(panel, GroupLayout.PREFERRED_SIZE, 808, GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 807, GroupLayout.PREFERRED_SIZE))))
                                .addContainerGap(93, Short.MAX_VALUE))
        );
        groupLayout.setVerticalGroup(
                groupLayout.createParallelGroup(Alignment.LEADING)
                        .addGroup(groupLayout.createSequentialGroup()
                                .addGap(72)
                                .addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
                                        .addComponent(label, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(BI_nameText, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(button, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE))
                                .addGap(64)
                                .addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 241, GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(ComponentPlacement.RELATED, 50, Short.MAX_VALUE)
                                .addComponent(panel, GroupLayout.PREFERRED_SIZE, 560, GroupLayout.PREFERRED_SIZE)
                                .addGap(39))
        );

        insurance_table = new JTable();
        insurance_table.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent me) {
                tableMousePressed(me);
            }
        });
        insurance_table.setModel(new DefaultTableModel(
                new Object[][]{
                },
                new String[]{
                        "保险编号", "保险名称", "保险项目", "适用人群", "保险金额", "保险年限"
                }
        ));
        insurance_table.getColumnModel().getColumn(0).setPreferredWidth(124);
        insurance_table.getColumnModel().getColumn(1).setPreferredWidth(141);
        insurance_table.getColumnModel().getColumn(2).setPreferredWidth(170);
        insurance_table.getColumnModel().getColumn(3).setPreferredWidth(159);
        insurance_table.getColumnModel().getColumn(4).setPreferredWidth(156);
        insurance_table.getColumnModel().getColumn(5).setPreferredWidth(139);
        insurance_table.setFont(new Font("宋体", Font.PLAIN, 16));
        insurance_table.setFillsViewportHeight(true);
        scrollPane.setViewportView(insurance_table);
        getContentPane().setLayout(groupLayout);

        this.fillTable(new Insurance());
    }

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    BuyInsurance frame = new BuyInsurance();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * 购买事件处理
     *
     * @param e
     */
    private void buyActionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        String insurance_id = this.insurance_idText.getText();
        String insurance_amount = this.insurance_amountText.getText();
        if (StringUtils.isEmpty(insurance_id)) {
            JOptionPane.showMessageDialog(null, "请选择要购买的保险产品！");
            return;
        }
        String client_name = this.client_nameText.getText();
        String client_password = this.client_passwordText.getText();

        if (StringUtils.isEmpty(client_name)) {
            JOptionPane.showMessageDialog(null, "请输入用户姓名！");
            return;
        }

        if (StringUtils.isEmpty(client_password)) {
            JOptionPane.showMessageDialog(null, "请输入用户密码！");
            return;
        }

        ClientDAO clientDao = new ClientDAO();
        Client client = clientDao.queryClient(new Client(client_name)).get(0);
        Integer client_id = client.getClient_id();
        String password = client.getClient_password();


        if (password.equals(client_password)) {
            Insurance insurance = insuranceDAO.queryInsurance(new Insurance(Integer.parseInt(insurance_id))).get(0);
            Buy_Insurance buy_insurance = buy_insuranceDAO.buyInsurance(client, insurance);
            if (buy_insurance != null) {
                JOptionPane.showMessageDialog(null, "购买成功！");
                resetValue();
            } else {
                JOptionPane.showMessageDialog(null, "购买失败！");
            }
        } else {
            JOptionPane.showMessageDialog(null, "密码错误！");
            this.client_passwordText.setText("");
            return;
        }
    }


    private void resetValue() {
        // TODO Auto-generated method stub
        this.insurance_idText.setText("");
        this.insurance_nameText.setText("");
        this.insurance_projectText.setText("");
        this.insurance_personText.setText("");
        this.insurance_amountText.setText("");
        this.insurance_yearText.setText("");
        this.client_nameText.setText("");
        this.client_passwordText.setText("");
    }

    /**
     * 表格行点击事件处理
     *
     * @param tme
     */
    private void tableMousePressed(MouseEvent tme) {
        // TODO Auto-generated method stub
        int row = this.insurance_table.getSelectedRow();
        this.insurance_idText.setText(String.valueOf(insurance_table.getValueAt(row, 0)));
        this.insurance_nameText.setText(String.valueOf(insurance_table.getValueAt(row, 1)));
        this.insurance_projectText.setText(String.valueOf(insurance_table.getValueAt(row, 2)));
        this.insurance_personText.setText(String.valueOf(insurance_table.getValueAt(row, 3)));
        this.insurance_amountText.setText(String.valueOf(insurance_table.getValueAt(row, 4)));
        this.insurance_yearText.setText(String.valueOf(insurance_table.getValueAt(row, 5)));
    }

    /**
     * 查询事件处理
     *
     * @param evt
     */
    private void queryActionPerformed(ActionEvent evt) {
        // TODO Auto-generated method stub
        String insurance_name = this.BI_nameText.getText();
        Insurance insurance = new Insurance();
        insurance.setInsurance_name(insurance_name);
        this.fillTable(insurance);
    }

    /**
     * 初始化表格
     *
     * @param insurance
     */
    private void fillTable(Insurance insurance) {
        DefaultTableModel dtm = (DefaultTableModel) insurance_table.getModel();
        dtm.setRowCount(0);//清空表格
        List<Insurance> insuranceList = insuranceDAO.queryInsurance(insurance);
        for (Insurance rs : insuranceList) {
            Vector v = new Vector();
            String up = rs.getInsurance_up();
            if ("1".equals(up)) {
                v.add(rs.getInsurance_id());
                v.add(rs.getInsurance_name());
                v.add(rs.getInsurance_project());
                v.add(rs.getInsurance_person());
                v.add(rs.getInsurance_amount());
                v.add(rs.getInsurance_year());
                dtm.addRow(v);
            }
        }

    }
}
